﻿using DevExpress.XtraGrid.Demos.util;
using DiaoPaiDaYin;
using DXApplicationTangche.DTO;
using DXApplicationTangche.service;
using DXApplicationTangche.UC.面料库存.面料出库.service;
using mendian;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DXApplicationTangche.UC.面料库存.库存记录.service
{
    public class 面料记录Service
    {
        /// <summary>
        /// 调webservice取值
        /// </summary>
        /// <returns></returns>
        public static List<DTO面料库存> get面料一览()
        {
            return
                FunctionHelper.JsonDeserialization<List<DTO面料库存>>(
                    RestCall.httpGetMethod(
                        URL.接口url + "/inventory/getInventoryByMetarialAll"
                        )
                    );
        }

        /// <summary>
        /// 调webservice取值
        /// </summary>
        /// <returns></returns>
        public static DTO面料库存 get面料详情(string id)
        {
            return
                FunctionHelper.JsonDeserialization<DTO面料库存>(
                    RestCall.httpGetMethod(
                        URL.接口url + "/inventory/getInventoryByMetarialId?materialId=" + id
                        )
                    );
        }
        /// <summary>
        /// 根据物料号查找物料信息
        /// </summary>
        /// <param name="materialNo"></param>
        /// <returns></returns>
        public static DTO面料库存 getMaterialInfoByMaterialNo(string materialNo)
        {
            return
                FunctionHelper.JsonDeserialization<DTO面料库存>(
                    RestCall.httpGetMethod(
                        URL.接口url + "/material/getMaterialInfoByMaterialNo?materialNo=" + materialNo
                        )
                    );
        }

        /// <summary>
        /// 调webservice取值
        /// </summary>
        /// <returns></returns>
        public static String post更新面料详情(DTO面料库存 DTO面料库存)
        {
            String re =
                    RestCall.PostHttp(
                        URL.接口url + "/material/saveMaterial",
                        FunctionHelper.JsonSerialization(
                            DTO面料库存
                            )
                        );
            return re;
        }

        /// <summary>
        /// 调webservice取值
        /// </summary>
        /// <returns></returns>
        public static List<DTO异常> get异常调整(string orderId)
        {
            return
                FunctionHelper.JsonDeserialization<List<DTO异常>>(
                    RestCall.httpGetMethod(
                        URL.接口url + "/inventory/getApplyJustifyByOrderId?orderId= " + orderId
                        )
                    );
        }

        /// <summary>
        /// get面料图
        /// </summary>
        /// <param name="styleId"></param>
        /// <returns></returns>
        public static String get面料图(string materialNo)
        {
            return
                URL.图片url + URL.面料图片路径 + RestCall.httpGetMethod(URL.接口url + "/image/getImagePath?materialCode=" + materialNo);
        }

        /// <summary>
        /// 修改成分
        /// </summary>
        /// <param name="materialCode"></param>
        /// <param name="composition"></param>
        /// <returns></returns>
        public static String updateComposition(String materialCode, String composition)
        {
            return
                RestCall.httpGetMethod(
                    URL.接口url + "/material/updateComposition?materialCode=" + materialCode + "&composition=" + composition
                );
        }
        /// <summary>
        /// 查询可用物料
        /// </summary>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public static List<MaterialPic> getMaterialDTO(String page, String size)
        {
            return
                FunctionHelper.JsonDeserialization<List<MaterialPic>>(
                    RestCall.httpGetMethod(
                        URL.接口url + "/material/getKingDeeMaterialInfoAll?page=" + page + "&size=" + size
                        )
                    );
        }
    }
    public class DTO日记账
    {
        public String code { get; set; }
        public String create_date { get; set; }
        public String fabric { get; set; }  //面料id
        public String id { get; set; }
        public String inout_type { get; set; }
        public Double price { get; set; }
        public Double quantity { get; set; }   //进出库数量
        public Double totalQuanlity { get; set; }  //结余数量
        public String orderId { get; set; }   //订单id
        public String orderNo { get; set; }    //订单号
        public DTO日记账()
        {
        }
    }
    public class DTO库存记录
    {
        public String id { get; set; }
        public String fabric { get; set; }
        public String quantity { get; set; }
        public String inout_type { get; set; }
        public String create_date { get; set; }
        public String code { get; set; }
        public String price { get; set; }
        public String orderId { get; set; }   //订单id
        public String orderNo { get; set; }    //订单号
        public DTO库存记录()
        {

        }
    }

    public class DTO面料库存
    {
        public Double unitPrice { get; set; }   //库存成本
        public String id { get; set; }    //  ID
        public String vendorName { get; set; }    //  供货商
        public String materialNo { get; set; }    //  面料号
        public String meterialDesc { get; set; }    //  描述名称
        public String composition { get; set; }    //  成分
        public String density { get; set; }    //  纱织密度
        public String weight { get; set; }    //  克重
        public String width { get; set; }    //  幅宽
        public String thick { get; set; }    //  厚度
        public String elastic { get; set; }    //  弹力
        public String feature { get; set; }    //  面料特性注意
        public String continuity { get; set; }    //  延续性
        public String uasge { get; set; }    //  用途
        public String inspectionResults { get; set; }    //  验料结果
        public String color { get; set; }    //  颜色
        public String season { get; set; }    //  季节
        public String style { get; set; }    //  风格
        public String arrangementMethod { get; set; }    //  整理方式
        public Double fabricQuanlity { get; set; } //库存数量

        public DTO面料库存()
        {

        }
        public DTO面料库存(DataRow dataRow)
        {
            this.materialNo = dataRow["MATERIAL_CODE"].ToString();
        }

        public DTO面料库存(string vendorName, string materialNo, string meterialDesc, string composition, string density, string weight, string width, string thick, string elastic, string feature, string continuity, string uasge, string inspectionResults, string color, string season, string style, string arrangementMethod)
        {
            this.vendorName = vendorName;
            this.materialNo = materialNo;
            this.meterialDesc = meterialDesc;
            this.composition = composition;
            this.density = density;
            this.weight = weight;
            this.width = width;
            this.thick = thick;
            this.elastic = elastic;
            this.feature = feature;
            this.continuity = continuity;
            this.uasge = uasge;
            this.inspectionResults = inspectionResults;
            this.color = color;
            this.season = season;
            this.style = style;
            this.arrangementMethod = arrangementMethod;
        }
    }
    public class DTO采购分录 : DTO面料库存
    {
        private DataRow dataRow;

        public double quantity { get; set; }    //  数量
        public double purchasePrice { get; set; }   //  采购单价

        public DTO采购分录(
            String id
            , String materialNo
            , String meterialDesc
            , String composition
            , String width
            , double quantity
            , double purchasePrice
            )
        {
            this.id = id;
            this.materialNo = materialNo;
            this.meterialDesc = meterialDesc;
            this.composition = composition;
            this.width = width;
            this.quantity = quantity;
            this.purchasePrice = purchasePrice;
        }

        public DTO采购分录(
            String id
            , String materialNo
            , String meterialDesc
            , String composition
            , String width
            , double quantity
            , string unitPrice
            )
        {
            this.id = id;
            this.materialNo = materialNo;
            this.meterialDesc = meterialDesc;
            this.composition = composition;
            this.width = width;
            this.quantity = quantity;
            this.unitPrice = Convert.ToDouble(unitPrice);
        }

        public DTO采购分录(DataRow dataRow)
        {
            this.dataRow = dataRow;
        }
    }

    public class PurchaseOrderDTO
    {
        public String orderId { get; set; } //  采购订单id
        public DateTime orderDate { get; set; } //  采购时间
        public DateTime purchaseStockInDate { get; set; }   //  采购入库时间
        public Double totalPriceWithTax { get; set; }   //  含税价格
        public Double transitExpenses { get; set; }  //  在途费用
        public List<PurchaseOrderEntryDTO> purchaseOrderEntryDTOS { get; set; } = new List<PurchaseOrderEntryDTO>();

        public PurchaseOrderDTO(DateTime orderDate, Double totalPriceWithTax, Double transitExpenses, List<DTO采购分录> DTO采购分录s)
        {
            this.orderId = new FunctionHelper().Uuid;
            this.purchaseStockInDate = this.orderDate = orderDate;
            this.totalPriceWithTax = totalPriceWithTax;
            this.transitExpenses = transitExpenses;
            foreach (DTO采购分录 DTO采购分录 in DTO采购分录s)
            {
                purchaseOrderEntryDTOS.Add(new PurchaseOrderEntryDTO(DTO采购分录));
            }
        }
    }

    public class PurchaseOrderEntryDTO
    {
        public String entryId { get; set; } //  采购分录ID
        public String materialId { get; set; }    //  物料id
        public String materialCode { get; set; }    //  物料编码
        public double quantity { get; set; }    //  数量
        public double purchasePrice { get; set; }   //  采购单价
        public double stockInUnitPrice { get; set; }   //  入库加权平均价格

        public PurchaseOrderEntryDTO(DTO采购分录 DTO采购分录)
        {
            this.entryId = new FunctionHelper().Uuid;
            this.materialId = DTO采购分录.id;
            this.materialCode = DTO采购分录.materialNo;
            this.quantity = DTO采购分录.quantity;
            this.purchasePrice = DTO采购分录.purchasePrice;
        }
    }

    public class MaterialPic
    {
        public String materialNo { get; set; }
        public String materialName { get; set; }
        public String materialImagePath { get; set; }
        public Image pic { get; set; }

        public MaterialPic(String materialNo, String materialName, String materialImagePath)
        {
            this.materialNo = materialNo;
            this.materialName = materialName;
            this.materialImagePath = materialImagePath;
            this.getPic();
        }
        public void getPic()
        {
            try
            {
                this.pic = FileService.GetImageByUrl(this.materialImagePath);
            }
            catch { }
        }
    }
}
